package com.pur.zsms.service.impl;

import java.io.InputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import net.sf.cglib.beans.BeanCopier;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.common.support.PageController;
import com.pur.zsms.common.support.PaginationSupport;
import com.pur.zsms.common.util.ExcelReader;
import com.pur.zsms.common.util.PubFun;
import com.pur.zsms.common.vo.CommonType;
import com.pur.zsms.dao.IDistributorDao;
import com.pur.zsms.dao.impl.DistributorDaoImpl;
import com.pur.zsms.foodmanage.food.po.FoodMaterPo;
import com.pur.zsms.foodmanage.food.po.FoodPo;
import com.pur.zsms.foodmanage.food.vo.FoodVo;
import com.pur.zsms.matermanage.mater.dao.IMaterDao;
import com.pur.zsms.matermanage.mater.po.MaterLevelBlPo;
import com.pur.zsms.matermanage.mater.po.MaterLevelPo;
import com.pur.zsms.matermanage.mater.po.MaterPo;
import com.pur.zsms.matermanage.mater.vo.MaterVo;
import com.pur.zsms.matermanage.mater.vo.MaterVoForSup;
import com.pur.zsms.matermanage.mater.vo.OrderDistributorMaterRealVo;
import com.pur.zsms.ordercustommanage.dao.ISelectFoodDao;
import com.pur.zsms.ordercustommanage.po.OrderCustFoodPo;
import com.pur.zsms.ordercustommanage.po.OrderCustPo;
import com.pur.zsms.ordercustommanage.po.OrderDistributorMaterRealPo;
import com.pur.zsms.ordercustommanage.po.OrderDistributorPo;
import com.pur.zsms.ordercustommanage.po.OrderSupplierMaterNeedPo;
import com.pur.zsms.ordercustommanage.po.OrderSupplierMaterPo;
import com.pur.zsms.ordercustommanage.po.OrderSupplierMaterRealPo;
import com.pur.zsms.ordercustommanage.po.OrderSupplierPo;
import com.pur.zsms.ordercustommanage.service.ISelectFoodService;
import com.pur.zsms.ordercustommanage.vo.OrderCustFoodVo;
import com.pur.zsms.ordercustommanage.vo.OrderCustVo;
import com.pur.zsms.ordercustommanage.vo.OrderDistributorVo;
import com.pur.zsms.ordercustommanage.vo.OrderStateVo;
import com.pur.zsms.ordercustommanage.vo.OrderSupplierMaterRealVo;
import com.pur.zsms.ordercustommanage.vo.OrderSupplierVo;
import com.pur.zsms.ordercustommanage.vo.SelectFoodVo;
import com.pur.zsms.ordercustommanage.vo.SelectMaterVo;
import com.pur.zsms.pmsnmanage.user.po.UserPo;
import com.pur.zsms.pmsnmanage.user.service.IUserService;
import com.pur.zsms.service.IDistributorService;
import com.pur.zsms.shopmanage.dao.IShopDao;
import com.pur.zsms.supplier.dao.SupplierDao;
import com.pur.zsms.supplier.dao.SupplierOrderDao;
import com.pur.zsms.supplier.po.ShopSupplierTypePo;
import com.pur.zsms.supplier.po.SupplierPo;
import com.pur.zsms.weixin.service.IWeixinService;

@Service("distributorService")
public class DistributorServiceImpl implements IDistributorService {

	final String dayNames[] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };

	@Resource(name = "distributorDao")
	private IDistributorDao distributorDao;

	@Resource(name = "selectFoodDao")
	private ISelectFoodDao selectFoodDaoImpl;

	@Resource(name = "materDao")
	private IMaterDao materDaoImpl;

	@Resource(name = "shopDao")
	private IShopDao shopDaoImpl;

	@Resource(name = "distributorService")
	private IDistributorService distributorService;

	@Resource(name = "weixinService")
	private IWeixinService weixinService;

	@Resource(name = "userService")
	private IUserService userService;

	@Resource(name = "supplierDao")
	private SupplierDao supplierDao;

	String msg = "";

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public PaginationSupport findMaterForDistributor(PageController pageController, MaterVo materVo) throws GenericException {
		return distributorDao.listMaterForDistributor(pageController, materVo);

	}

	public OrderDistributorPo addMaterOrderForDistributor(List<OrderDistributorMaterRealVo> orderDistributorMaterRealVoList) throws GenericException {
		OrderDistributorPo orderDistributorPo = new OrderDistributorPo();
		orderDistributorPo.setOrdSupId(orderDistributorMaterRealVoList.get(0).getOrdSupId());
		orderDistributorPo.setOrdDistributorId(orderDistributorMaterRealVoList.get(0).getOrdDistributorId());
		// 跳过审核状态
		orderDistributorPo.setOrdStateDistributor(1);
		orderDistributorPo.setOrdDate(orderDistributorMaterRealVoList.get(0).getOrderDate());
		orderDistributorPo.setOrdDay(orderDistributorMaterRealVoList.get(0).getOrderDay());
		distributorDao.addOrderDistributorPo(orderDistributorPo);
		return orderDistributorPo;
	}

	@Override
	public void addMaterOrderDetailForDistributor(List<OrderDistributorMaterRealVo> orderDistributorMaterRealVoList, int ordId)
			throws GenericException {
		for (int i = 0; i < orderDistributorMaterRealVoList.size(); i++) {
			OrderDistributorMaterRealPo orderDistributorMaterRealPo = new OrderDistributorMaterRealPo();
			orderDistributorMaterRealPo.setOrdId(ordId);
			orderDistributorMaterRealPo.setOrdMaterId(orderDistributorMaterRealVoList.get(i).getOrdMaterId());
			orderDistributorMaterRealPo.setOrdMaterLevel(orderDistributorMaterRealVoList.get(i).getOrdMaterLevel());
			orderDistributorMaterRealPo.setOrdMaterWeight(orderDistributorMaterRealVoList.get(i).getOrdMaterWeight());
			orderDistributorMaterRealPo.setOrdMaterWeightReality(orderDistributorMaterRealVoList.get(i).getOrdMaterWeight());
			orderDistributorMaterRealPo.setOrdMaterValueCost("0");
			orderDistributorMaterRealPo.setOrdMaterValueCostUnit("0");
			orderDistributorMaterRealPo.setOrdSupId(orderDistributorMaterRealVoList.get(i).getOrdSupId());
			orderDistributorMaterRealPo.setCode(orderDistributorMaterRealVoList.get(i).getCode());

			// 取得提交价格后的订单的最后一次报价
			List<OrderDistributorMaterRealPo> materValueLastPo = distributorDao
					.getOrderDistributorMaterRealPoForLastMaterValue(orderDistributorMaterRealVoList.get(i));
			if (materValueLastPo != null && materValueLastPo.size() > 0) {
				orderDistributorMaterRealPo.setOrdMaterValueLast(materValueLastPo.get(0).getOrdMaterValueUnit().toString());
				orderDistributorMaterRealPo.setOrdMaterValueUnit(new BigDecimal(orderDistributorMaterRealPo.getOrdMaterValueLast()));
				orderDistributorMaterRealPo.setOrdMaterValue(new BigDecimal(orderDistributorMaterRealPo.getOrdMaterValueLast())
						.multiply(orderDistributorMaterRealVoList.get(i).getOrdMaterWeight()));

			} else {
				orderDistributorMaterRealPo.setOrdMaterValueLast("0");
				orderDistributorMaterRealPo.setOrdMaterValueUnit(new BigDecimal(0));
				orderDistributorMaterRealPo.setOrdMaterValue(new BigDecimal(0));

			}
			distributorDao.addOrderDistributorMaterRealPo(orderDistributorMaterRealPo);
		}

	}

	public PaginationSupport listDistributorOrder(PageController pageController, OrderDistributorMaterRealVo orderDistributorMaterRealVo)
			throws GenericException {
		return distributorDao.listDistributorOrder(pageController, orderDistributorMaterRealVo);
	}

	public OrderDistributorPo getOrderDistributorPo(OrderDistributorVo orderDistributorVo) throws GenericException {
		return distributorDao.getOrderDistributorPo(orderDistributorVo).get(0);
	}

	public OrderDistributorMaterRealPo getOrderDistributorMaterRealPo(OrderDistributorMaterRealVo tOrderDistributorMaterRealVo)
			throws GenericException {

		List<OrderDistributorMaterRealPo> list = distributorDao.getOrderDistributorMaterRealPo2(tOrderDistributorMaterRealVo);
		if (list != null && list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}

	public PaginationSupport findDistributorDetailOrder(PageController pageController, OrderDistributorMaterRealVo orderDistributorMaterRealVo)
			throws GenericException {
		return distributorDao.findDistributorDetailOrder(pageController, orderDistributorMaterRealVo);
	}

	public PaginationSupport findDistributorDetailOrderForSup(PageController pageController, OrderDistributorMaterRealVo orderDistributorMaterRealVo)
			throws GenericException {
		return distributorDao.findDistributorDetailOrderForSup(pageController, orderDistributorMaterRealVo);
	}

	public void updateOrderForDistributor(OrderDistributorMaterRealVo orderDistributorMaterRealVo) throws GenericException {
		JSONArray array = JSONArray.fromObject(orderDistributorMaterRealVo.getMaterdategridList());

		for (int i = 0; i < array.size(); i++) {
			JSONObject object = JSONObject.fromObject(array.get(i));

			OrderDistributorMaterRealPo tOrderDistributorMaterRealPo = new OrderDistributorMaterRealPo();
			tOrderDistributorMaterRealPo.setOrdId(orderDistributorMaterRealVo.getOrdId());
			tOrderDistributorMaterRealPo.setOrdSupId(orderDistributorMaterRealVo.getOrdSupId());
			tOrderDistributorMaterRealPo.setOrdMaterId(Integer.valueOf(object.get("ordMaterId").toString()));
			tOrderDistributorMaterRealPo.setOrdMaterLevel(Integer.valueOf(object.get("ordMaterLevel").toString()));
			tOrderDistributorMaterRealPo.setOrdDistributorMaterId(Integer.valueOf(object.get("ordDistributorMaterId").toString()));
			// 取得要更改的数据
			tOrderDistributorMaterRealPo = distributorDao.getOrderDistributorMaterRealPo(tOrderDistributorMaterRealPo).get(0);
			tOrderDistributorMaterRealPo.setOrdMaterWeightReality(new BigDecimal(object.get("ordMaterWeightReality").toString()));
			tOrderDistributorMaterRealPo.setOrdMaterValueUnit(new BigDecimal(object.get("ordMaterValueUnit").toString()));
			tOrderDistributorMaterRealPo.setOrdMaterValue(tOrderDistributorMaterRealPo.getOrdMaterWeightReality().multiply(
					tOrderDistributorMaterRealPo.getOrdMaterValueUnit()));
			distributorDao.updateOrderDistributorMaterRealPo(tOrderDistributorMaterRealPo);
		}
	}

	public void updateOrderMaterValueAll(OrderDistributorPo tOrderDistributorPo, boolean flag) throws GenericException {
		List<Object> list = distributorDao.getOrderDistributorMaterValueAll(tOrderDistributorPo.getOrdId());
		if (list != null && list.size() > 0 && list.get(0) != null && !list.get(0).equals("null")) {
			tOrderDistributorPo.setOrdValue(new BigDecimal(list.get(0).toString()));
		} else {
			tOrderDistributorPo.setOrdValue(new BigDecimal(0));
		}
		if (flag) {
			tOrderDistributorPo.setOrdStateDistributor(CommonType.ORD_STATE_DISTRIBUTOR_2);
		}
		distributorDao.updateOrderDistributorPo(tOrderDistributorPo);
	}

	public void deleteOrderMaterForDistributor(OrderDistributorMaterRealVo orderDistributorMaterRealVo) throws GenericException {
		JSONArray array = JSONArray.fromObject(orderDistributorMaterRealVo.getMaterdategridList());
		for (int i = 0; i < array.size(); i++) {
			JSONObject object = JSONObject.fromObject(array.get(i));

			OrderDistributorMaterRealPo tOrderDistributorMaterRealPo = new OrderDistributorMaterRealPo();
			tOrderDistributorMaterRealPo.setOrdId(orderDistributorMaterRealVo.getOrdId());
			tOrderDistributorMaterRealPo.setOrdSupId(orderDistributorMaterRealVo.getOrdSupId());
			tOrderDistributorMaterRealPo.setOrdDistributorMaterId(Integer.valueOf(object.get("ordDistributorMaterId").toString()));

			tOrderDistributorMaterRealPo.setOrdMaterId(Integer.valueOf(object.get("ordMaterId").toString()));
			tOrderDistributorMaterRealPo.setOrdMaterLevel(Integer.valueOf(object.get("ordMaterLevel").toString()));
			distributorDao.deleteOrderMaterForDistributor(tOrderDistributorMaterRealPo);
		}
	}

	public void deleteOrderForDistributor(OrderDistributorMaterRealVo orderDistributorMaterRealVo) throws GenericException {
		distributorDao.deleteOrderForDistributor(orderDistributorMaterRealVo);
	}

	public void reviewOrderDistributor(OrderDistributorPo orderDistributorPo) throws GenericException {
		orderDistributorPo.setOrdStateDistributor(CommonType.ORD_STATE_DISTRIBUTOR_3);
		distributorDao.updateOrderDistributorPo(orderDistributorPo);
	}

	/** 按照sheet 页分供货商 */
	public boolean importCommitMaterOrderForDistributorToSheet(ExcelReader excelReader, InputStream is, long distributorId, String ordDate)
			throws Exception {
		// 得到sheet页的数量
		int sheetNum = excelReader.readSheetNum(is);
		if (sheetNum == 0) {
			msg = "没有数据，请确认文件是否正确！";
			return false;
		}
		// 检验数据格式

		if (!checkForImportCommitMaterOrderForDistributor(sheetNum, excelReader, is)) {
			return false;
		}

		// 检验完毕 开始业务逻辑

		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		date = format.parse(ordDate);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
		if (dayOfWeek < 0)
			dayOfWeek = 0;

		for (int j = 0; j < sheetNum; j++) {
			OrderDistributorPo orderDistributorPo = new OrderDistributorPo();
			orderDistributorPo.setOrdSupId(0);
			orderDistributorPo.setOrdDistributorId(distributorId);
			// 跳过审核状态
			orderDistributorPo.setOrdStateDistributor(1);
			orderDistributorPo.setOrdDate(ordDate);
			orderDistributorPo.setOrdDay(dayNames[dayOfWeek]);
			orderDistributorPo.setOrdDescribe(excelReader.getSheetName(is, j));
			distributorDao.addOrderDistributorPo(orderDistributorPo);

			Map<Integer, String> map = excelReader.readExcelContent(is, "&", j);
			for (int i = 1; i <= map.size(); i++) {
				String[] everyline = map.get(i).split("\\&");
				String materId = everyline[1];
				String materNum = everyline[8];
				String materLevel = everyline[9];

				OrderDistributorMaterRealPo orderDistributorMaterRealPo = new OrderDistributorMaterRealPo();
				orderDistributorMaterRealPo.setOrdId(orderDistributorPo.getOrdId());
				orderDistributorMaterRealPo.setOrdMaterId(Integer.valueOf(materId));
				orderDistributorMaterRealPo.setOrdMaterLevel(Integer.valueOf(materLevel));
				orderDistributorMaterRealPo.setOrdMaterWeight(new BigDecimal(materNum));
				orderDistributorMaterRealPo.setOrdMaterWeightReality(new BigDecimal(materNum));
				orderDistributorMaterRealPo.setOrdMaterValue(new BigDecimal(0));
				orderDistributorMaterRealPo.setOrdMaterValueCost("0");
				orderDistributorMaterRealPo.setOrdMaterValueCostUnit("0");
				orderDistributorMaterRealPo.setOrdSupId(0);
				orderDistributorMaterRealPo.setCode("");
				distributorDao.addOrderDistributorMaterRealPo(orderDistributorMaterRealPo);
			}
		}
		return true;
	}

	/** 按照供货商分供货商 */
	public boolean importCommitMaterOrderForDistributorToSup(ExcelReader excelReader, InputStream is, long distributorId, String ordDate)
			throws Exception {
		// 得到sheet页的数量
		int sheetNum = excelReader.readSheetNum(is);
		if (sheetNum == 0) {
			msg = "没有数据，请确认文件是否正确！";
			return false;
		}
		// 检验数据格式

		if (!checkForImportCommitMaterOrderForDistributor(sheetNum, excelReader, is)) {
			return false;
		}

		// 检验完毕 开始业务逻辑
		// 把所有数据转换成map key是供货商id value 是list合集
		Map<String, List<OrderDistributorMaterRealPo>> maplist = new HashMap<String, List<OrderDistributorMaterRealPo>>();
		for (int j = 0; j < sheetNum; j++) {
			Map<Integer, String> map = excelReader.readExcelContent(is, "&", j);
			for (int i = 1; i <= map.size(); i++) {
				String[] everyline = map.get(i).split("\\&");
				String materId = everyline[1];
				String materNum = everyline[8];
				String materLevel = everyline[9];
				String supName = everyline[10];
				int supId = supplierDao.getSupPoByName(supName).get(0).getSupplierId();

				OrderDistributorMaterRealPo orderDistributorMaterRealPo = new OrderDistributorMaterRealPo();
				orderDistributorMaterRealPo.setOrdId(0);// 待设定
				orderDistributorMaterRealPo.setOrdMaterId(Integer.valueOf(materId));
				orderDistributorMaterRealPo.setOrdMaterLevel(Integer.valueOf(materLevel));
				orderDistributorMaterRealPo.setOrdMaterWeight(new BigDecimal(materNum));
				orderDistributorMaterRealPo.setOrdMaterWeightReality(new BigDecimal(materNum));
				orderDistributorMaterRealPo.setOrdMaterValueCost("0");
				orderDistributorMaterRealPo.setOrdMaterValueCostUnit("0");
				orderDistributorMaterRealPo.setOrdSupId(supId);
				orderDistributorMaterRealPo.setCode("");

				// 取得提交价格后的订单的最后一次报价
				OrderDistributorMaterRealVo orderDistributorMaterRealPoForMaterLast = new OrderDistributorMaterRealVo();
				orderDistributorMaterRealPoForMaterLast.setOrdMaterId(Integer.valueOf(materId));
				orderDistributorMaterRealPoForMaterLast.setOrdSupId(supId);
				List<OrderDistributorMaterRealPo> materValueLastPo = distributorDao
						.getOrderDistributorMaterRealPoForLastMaterValue(orderDistributorMaterRealPoForMaterLast);
				if (materValueLastPo != null && materValueLastPo.size() > 0) {
					orderDistributorMaterRealPo.setOrdMaterValueLast(materValueLastPo.get(0).getOrdMaterValueUnit().toString());
					orderDistributorMaterRealPo.setOrdMaterValueUnit(new BigDecimal(orderDistributorMaterRealPo.getOrdMaterValueLast()));
					orderDistributorMaterRealPo.setOrdMaterValue(new BigDecimal(orderDistributorMaterRealPo.getOrdMaterValueLast())
							.multiply(new BigDecimal(materNum)));

				} else {
					orderDistributorMaterRealPo.setOrdMaterValueLast("0");
					orderDistributorMaterRealPo.setOrdMaterValueUnit(new BigDecimal(0));
					orderDistributorMaterRealPo.setOrdMaterValue(new BigDecimal(0));

				}

				if (maplist.containsKey(String.valueOf(supId))) {
					List<OrderDistributorMaterRealPo> orderDistributorMaterRealPoList = maplist.get(String.valueOf(supId));
					orderDistributorMaterRealPoList.add(orderDistributorMaterRealPo);
				} else {
					List<OrderDistributorMaterRealPo> orderDistributorMaterRealPoList = new ArrayList<OrderDistributorMaterRealPo>();
					orderDistributorMaterRealPoList.add(orderDistributorMaterRealPo);
					maplist.put(String.valueOf(supId), orderDistributorMaterRealPoList);
				}

			}
		}

		// 准备完数据插入数据库
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		date = format.parse(ordDate);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
		if (dayOfWeek < 0)
			dayOfWeek = 0;

		for (Map.Entry<String, List<OrderDistributorMaterRealPo>> entry : maplist.entrySet()) {

			String supId = entry.getKey();

			OrderDistributorPo orderDistributorPo = new OrderDistributorPo();
			orderDistributorPo.setOrdSupId(Integer.valueOf(supId));
			orderDistributorPo.setOrdDistributorId(distributorId);
			// 跳过审核状态
			orderDistributorPo.setOrdStateDistributor(1);
			orderDistributorPo.setOrdDate(ordDate);
			orderDistributorPo.setOrdDay(dayNames[dayOfWeek]);
			distributorDao.addOrderDistributorPo(orderDistributorPo);
			for (OrderDistributorMaterRealPo orderDistributorMaterRealPo : entry.getValue()) {
				orderDistributorMaterRealPo.setOrdId(orderDistributorPo.getOrdId());
				distributorDao.addOrderDistributorMaterRealPo(orderDistributorMaterRealPo);
			}
			// 给供货商发送微信消息
			List<UserPo> userPoList = userService.getUserForSup(orderDistributorPo.getOrdSupId());
			for (UserPo userPo : userPoList) {
				if (userPo.getOpenId() != null && !userPo.getOpenId().equals("")) {
					weixinService.send_template_message_for_commitMaterOrderForDistributor("wx64bdfb1d6682c965", "84b1db883fbb66d23fd959ba418d2e38",
							userPo.getOpenId(), orderDistributorPo);
				}
			}
		}

		return true;
	}

	public boolean isNumeric(String str) {
		Pattern pattern = Pattern.compile("^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$");
		Matcher isNum = pattern.matcher(str);
		if (!isNum.matches()) {
			return false;
		}
		return true;
	}

	public boolean checkForImportCommitMaterOrderForDistributor(int sheetNum, ExcelReader excelReader, InputStream is) throws GenericException {
		for (int j = 0; j < sheetNum; j++) {
			Map<Integer, String> map = excelReader.readExcelContent(is, "&", j);
			for (int i = 1; i <= map.size(); i++) {
				String[] everyline = map.get(i).split("\\&");
				String materId = everyline[1];
				String materNum = everyline[8];
				String supName = everyline[10];
				if (everyline.length != 11) {
					msg = "材料" + materId + "格式有问题！必须保证每列都填满！";
					return false;
				}
				if (materNum.equals("0") || !isNumeric(materNum)) {
					msg = "材料" + materId + "采购数量必须为数字！且不能为0!";
					return false;
				}

				List<SupplierPo> poList = supplierDao.getSupPoByName(supName);
				if (poList == null || poList.size() != 1) {
					msg = "“" + supName + "”供货商不存在!或者系统中有多个相同名称的供货商！";
					return false;
				}

			}
		}
		return true;
	}

	public void updateOrderDistributorPo(OrderDistributorPo orderDistributorPo) throws GenericException {
		// 改变主表
		distributorDao.updateOrderDistributorPo(orderDistributorPo);
		// 改变分表
		OrderDistributorMaterRealPo orderDistributorMaterRealPo = new OrderDistributorMaterRealPo();
		orderDistributorMaterRealPo.setOrdId(orderDistributorPo.getOrdId());
		List<OrderDistributorMaterRealPo> list = distributorDao.getOrderDistributorMaterRealPo(orderDistributorMaterRealPo);
		for (OrderDistributorMaterRealPo orderDistributorMaterRealPo2 : list) {
			orderDistributorMaterRealPo2.setOrdSupId(orderDistributorPo.getOrdSupId());
			distributorDao.updateOrderDistributorMaterRealPo(orderDistributorMaterRealPo2);
		}
	}

	public boolean changeOrdMater(OrderDistributorMaterRealVo orderDistributorMaterRealVo) throws GenericException {
		// 校验分货商客户是否有订单
		List<Object[]> list = distributorDao.getOrderSupplierMaterForDistributor(orderDistributorMaterRealVo.getOrdMaterId(),
				orderDistributorMaterRealVo.getOrdMaterLevel(), orderDistributorMaterRealVo.getOrdDate(),
				(int) orderDistributorMaterRealVo.getOrdDistributorId());

		if (list == null || list.size() == 0) {
			msg = orderDistributorMaterRealVo.getOrdDate() + "该分销商此种材料没有客户下单！材料id为:" + orderDistributorMaterRealVo.getOrdMaterId();
			return false;
		}
		// 先删除分货商订单中此种材料的订单
		OrderDistributorMaterRealPo tOrderDistributorMaterRealPo = new OrderDistributorMaterRealPo();
		tOrderDistributorMaterRealPo.setOrdMaterId(orderDistributorMaterRealVo.getOrdMaterId());
		tOrderDistributorMaterRealPo.setOrdMaterLevel(orderDistributorMaterRealVo.getOrdMaterLevel());
		tOrderDistributorMaterRealPo.setOrdId(orderDistributorMaterRealVo.getOrdId());
		distributorDao.deleteOrderMaterForDistributor(tOrderDistributorMaterRealPo);
		// 插入新的材料
		if (list != null && list.size() > 0) {
			for (Object[] object : list) {
				OrderDistributorMaterRealPo po = new OrderDistributorMaterRealPo();
				po.setOrdMaterId(Integer.valueOf(object[0].toString()));
				po.setOrdMaterWeight(new BigDecimal(object[6].toString()));
				if (object[7] != null) {
					po.setCode(object[7].toString());
				} else {
					po.setCode("未设定");
				}
				po.setOrdId(orderDistributorMaterRealVo.getOrdId());
				po.setOrdMaterLevel(orderDistributorMaterRealVo.getOrdMaterLevel());
				po.setOrdMaterValue(new BigDecimal(orderDistributorMaterRealVo.getOrdMaterValueLast()).multiply(new BigDecimal(object[6].toString())));
				po.setOrdMaterValueUnit(new BigDecimal(orderDistributorMaterRealVo.getOrdMaterValueLast()));
				po.setOrdMaterWeightReality(new BigDecimal(object[6].toString()));
				po.setOrdSupId(orderDistributorMaterRealVo.getOrdSupId());
				po.setOrdMaterValueLast(orderDistributorMaterRealVo.getOrdMaterValueLast());
				distributorDao.addOrderDistributorMaterRealPo(po);
			}
		}

		return true;
	}

}
